package myJs.projectCps.zScoreHeatmapPlot

import cats.effect.SyncIO
import com.softwaremill.quicklens._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react._
import japgolly.scalajs.react.vdom.all._
import myJs.cps._
import myJs.tool.Tool._

import scala.collection.immutable.SeqMap

/** Created by yz on 3/11/2020
  */
object FontCp {

  val plotO = Cp.plotO

  case class Props(
      basicData: plotO.FontData,
      basicDataChange: (plotO.FontData => plotO.FontData) => SyncIO[Unit]
  ) {
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    object FTool {

      def strChange(f: PathLazyModify[plotO.FontData, String]) = (v: String) => {
        props.basicDataChange {
          f.setTo(v)
        }
      }

    }

    val basicData = props.basicData
    val fontFamilyMap = SeqMap(
      "Times" -> "Times New Roman（serif）",
      "ArialMT" -> "Arial（sans）",
      "mono" -> "Courier（mono）",
      "SimSun" -> "宋体（SimSun）"
    )
    val showRowNameMap = SeqMap("F" -> messages("no"), "T" -> messages("yes"))
    val yRotateMap = SeqMap("90" -> "-90", "45" -> "-45", "0" -> "0", "-45" -> "45", "-90" -> "90")

    Fragment(
      SelectInlineCp
        .Props(
          value = basicData.fontFamily,
          label = messages("font"),
          name = "fontFamily",
          optionMap = fontFamilyMap,
          labelWidth = 4,
          inputWidth = 8,
          onChange = FTool.strChange(modifyLens[plotO.FontData](_.fontFamily))
        )
        .render,
      SelectInlineCp
        .Props(
          value=basicData.showColName,
          label = messages("showColnames"),
          name = "showColName",
          optionMap = showRowNameMap,
          onChange = FTool.strChange(modifyLens[plotO.FontData](_.showColName)),
          labelWidth = 4,
          inputWidth = 8,
        )
        .render,
      InputInlineCp.Props(
        label = messages("xaxisTextSize"),
        name = "xFont",
        labelWidth = 4,
        inputWidth = 8,
        value = basicData.xFont,
        onChange = FTool.strChange(modifyLens[plotO.FontData](_.xFont))
      ).render,
      SelectInlineCp
        .Props(
          value=basicData.xRotate,
          onChange=FTool.strChange(modifyLens[plotO.FontData](_.xRotate)),
          label = messages("colnamesAngle"),
          name = "xRotate",
          optionMap = yRotateMap,
          labelWidth = 4,
          inputWidth = 8,
          show=(basicData.showColName == "T")
        )
        .render,
      SelectInlineCp
        .Props(
          value=basicData.showRowName,
          onChange=FTool.strChange(modifyLens[plotO.FontData](_.showRowName)),
          label = messages("showRownames"),
          name = "showRowName",
          optionMap = showRowNameMap,
          labelWidth = 4,
          inputWidth = 8,
        )
        .render,
      InputInlineCp
        .Props(
          label = messages("yAxisTextSize"),
          name = "yFont",
          labelWidth = 4,
          inputWidth = 8,
          value = basicData.yFont,
          onChange = FTool.strChange(modifyLens[plotO.FontData](_.yFont))
        )
        .render,
      SelectInlineCp
        .Props(
          value=basicData.yRotate,
          onChange=FTool.strChange(modifyLens[plotO.FontData](_.yRotate)),
          label = messages("rownamesAngle"),
          name = "yRotate",
          optionMap = yRotateMap,
          labelWidth = 4,
          inputWidth = 8,
          show = (basicData.showRowName == "T")
        )
        .render,
      InputInlineCp.Props(
        label = messages("legendTitleContent"),
        name = "legendTitle",
        labelWidth = 4,
        inputWidth = 8,
        value = basicData.legendTitle,
        onChange = FTool.strChange(modifyLens[plotO.FontData](_.legendTitle))
      ).render,
      InputInlineCp
        .Props(
          label = messages("legendTitleSize"),
          name = "legendTitleFont",
          labelWidth = 4,
          inputWidth = 8,
          value = basicData.legendTitleFont,
          onChange = FTool.strChange(modifyLens[plotO.FontData](_.legendTitleFont))
        )
        .render,
      InputInlineCp
        .Props(
          label = messages("legendTextSize"),
          name = "legendFont",
          labelWidth = 4,
          inputWidth = 8,
          value = basicData.legendFont,
          onChange = FTool.strChange(modifyLens[plotO.FontData](_.legendFont))
        )
        .render
    )

  }

}
